﻿using ERP.ERPModels.Production.MasterData;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.RBAC.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.HumanResources.Production.MasterData
{
    public class ProcessConfigurationService:ApplicationService, IProcessConfigurationService
    {
        /// <summary>
        /// 工序设置仓储
        /// </summary>
        private readonly IRepository<ProcessConfiguration,int> _processConfigurationRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processConfigurationRepository"></param>
        public ProcessConfigurationService(IRepository<ProcessConfiguration, int> processConfigurationRepository)
        {
            _processConfigurationRepository = processConfigurationRepository;
        }

        /// <summary>
        /// 获取工序配置
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public async Task<ProcessConfiguration> GetAsync(int id)
        {
            return await _processConfigurationRepository.GetAsync(id);
        }

        /// <summary>
        /// 创建工序配置
        /// </summary>
        /// <param name="input">创建参数</param>
        /// <returns></returns>1
        public async Task<int> AddProcessConfiguration(ProcessConfigurationAddDTO param)
        {
            var result = ObjectMapper.Map<ProcessConfigurationAddDTO, ProcessConfiguration>(param);
            var query = await _processConfigurationRepository.InsertAsync(result);
            return query == null ? 0 : 1;

        }

        /// <summary>
        /// 更新工序配置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="input">更新参数</param>
        /// <returns></returns>
        public async Task<int> UpdateProcessConfiguration(ProcessConfigurationUpdateDTO param)
        {
            var result = ObjectMapper.Map<ProcessConfigurationUpdateDTO, ProcessConfiguration>(param);
            var query = await _processConfigurationRepository.UpdateAsync(result);
            return query == null ? 0 : 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteProcessConfiguration(int Id)
        {
            try
            {
                var result = await _processConfigurationRepository.FindAsync(m => m.Id == Id);
                if (result == null)
                {
                    return 0;
                }
                await _processConfigurationRepository.DeleteAsync(result);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
    }
}
